﻿#region COPYRIGHT
//--------------------------------------------------------------------------------
// <copyright file="Camera2D.cs" company="starLiGHT Entertainment Studios">
//    Copyright (c) 2007, 2008, 2009, 2010
//       Roland Rosenkranz (Glatzemann@email.de)
// </copyright>
// <license>
//   This file is part of starLiGHT.Render.
//
//   starLiGHT.Render is free software: you can redistribute it and/or modify
//   it under the terms of the GNU Lesser General Public License as published by
//   the Free Software Foundation, either version 3 of the License, or
//   (at your option) any later version.
//
//   starLiGHT.Core is distributed in the hope that it will be useful,
//   but WITHOUT ANY WARRANTY; without even the implied warranty of
//   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//   GNU Lesser General Public License for more details.
//
//   You should have received a copy of the GNU Lesser General Public License
//   along with starLiGHT.Render. If not, see http://www.gnu.org/licenses/.
//    
//   ADDITIONAL (commercial) LICENSES for starLiGHT.Render are available on request.
// </license>
// <version>
// **************[ starLiGHT.Engine SVN ]**********************
// * $Rev:: 620                   $:  Revision of last commit *
// * $Author:: unknown            $:  Author of last commit   *
// * $Date:: 2010-12-07 15:23:58 #$:  Date of last commit     *
// ************************************************************
// </version>
//--------------------------------------------------------------------------------
#endregion

namespace starLiGHT.Camera.TwoD
{
    #region Using Statements
    using System;
    using System.Collections.Generic;
    using System.Text;
    using Microsoft.Xna.Framework;
    using Microsoft.Xna.Framework.Graphics;

    using starLiGHT.Collections;

    #endregion

    /// <summary>
    /// Basic camera class for 2D scenes.
    /// </summary>
    public class Camera2D : ICamera2D //////////TODO: re-integrate: , MovableObject2D
    {
        #region Member Variables
        private RectangleF bounds;
        private Vector2 viewportPosition;
        private Rectangle viewportRect;
        private float zoom = 1f;
        private float rotation = 0.0f;

        ////private SceneManager2D mSceneMgr;
        ////private SceneManagement.Viewport mLastViewport;
        private bool autoAspectRatio;

        private ICameraConstraint constraint;
        private ICameraController controller;

        private int visFacesLastRender;
        private int visBatchesLastRender;

        private Dictionary<int, float> parallaxModifier;
        #endregion

        /// <summary>
        /// Initializes a new instance of the Camera2D class.
        /// </summary>
        /// <param name="position">Position in 2-dimensional space of this camera.</param>
        /// <param name="size">Size of the visible area this camera covers.</param>
        public Camera2D(Vector2 position, Vector2 size)
        {
            this.bounds = new RectangleF(position, (position + size));
            this.viewportRect = new Rectangle(0, 0, (int)size.X, (int)size.Y);
            this.parallaxModifier = new Dictionary<int, float>();
        }

        ////public Camera2D(string name, SceneManager2D sceneManager)
        ////{
        ////    mName = name;
        ////    mSceneMgr = sceneManager;
        ////    parallaxModifier = new Dictionary<int, float>();
        ////}

        /// <summary>
        /// Gets or sets the position of the visible area covered by this camera.
        /// </summary>
        public Vector2 Anchor
        {
            get { return this.viewportPosition; }
            set { this.viewportPosition = value; }
        }

        /// <summary>
        /// Gets or sets the centered
        /// </summary>
        public Vector2 Center
        {
            get 
            {
                return this.viewportPosition + (this.ViewportSize * 0.5f); 
            }

            set
            {
                value -= this.ViewportSize * 0.5f;
                this.viewportPosition = value;
            }
        }

        public Vector2 ViewportPosition
        {
            get { return this.viewportPosition; }
            set { this.viewportPosition = value; }
        }

        public Vector2 ViewportSize
        {
            get { return new Vector2(this.viewportRect.Width / this.zoom, this.viewportRect.Height / this.zoom); }
        }

        public ICameraConstraint Constraint
        {
            get { return this.constraint; }
            set { this.constraint = value; }
        }

        public ICameraController Controller
        {
            get { return this.controller; }
            set { this.controller = value; }
        }

        public RectangleF Bounds
        {
            get { return this.bounds; }
            set { this.bounds = value; }
        }

        public Vector2 Size
        {
            get { return this.bounds.Size; }
        }

        public float Zoom
        {
            get { return this.zoom; }
            set { this.zoom = value; }
        }

        public float Rotation
        {
            get { return this.rotation; }
            set { this.rotation = value; }
        }

        public bool AutoAspectRatio
        {
            get { return this.autoAspectRatio; }
            set { this.autoAspectRatio = value; }
        }

        public int NumRenderedFaces
        {
            get { return this.visFacesLastRender; }
        }

        public int NumRenderedBatches
        {
            get { return this.visBatchesLastRender; }
        }

        public Matrix CameraMatrixNoScale
        {
            get
            {
                return Matrix.Identity *
                       Matrix.CreateTranslation(new Vector3(-this.Anchor, 0)) *
                       Matrix.CreateRotationZ(this.rotation) *
                       Matrix.CreateTranslation(new Vector3(this.Size * 0.5f, 0));
            }
        }

        ////public override RectangleF BoundingBox
        ////{
        ////    get { return bounds; }
        ////}

        ////public override float BoundingRadius
        ////{
        ////    get { throw new NotImplementedException(); }
        ////}

        ////public override void _updateRenderQueue(starLiGHT.SceneManagement.RenderQueue2D queue)
        ////{
        ////    throw new NotImplementedException();
        ////}

        ////public override string MovableType
        ////{
        ////    get { return "Camera2D"; }
        ////}

        ////public virtual void _renderScene(SceneManagement.Viewport vp, bool includeOverlays)
        ////{
        ////    ////////TODO: Listeners missing

        ////    mSceneMgr._renderScene(this, vp, includeOverlays);
        ////}

        ////public virtual void _notifyViewport(SceneManagement.Viewport viewport)
        ////{
        ////    mLastViewport = viewport;
        ////}

        ////public SceneManagement.Viewport Viewport
        ////{
        ////    get { return mLastViewport; }
        ////}

        public Matrix View
        {
            get 
            { 
                return this.GetCameraMatrix(0); 
            }
        }

        public Matrix Projection
        {
            get { throw new NotImplementedException(); }
        }

        public Matrix ViewProjection
        {
            get { throw new NotImplementedException(); }
        }

        public Matrix InverseViewProjection
        {
            get { throw new NotImplementedException(); }
        }

        public void _notifyRenderedFaces(int numfaces)
        {
            this.visFacesLastRender = numfaces;
        }

        public void _notifyRenderedBatches(int numbatches)
        {
            this.visBatchesLastRender = numbatches;
        }

        public float GetParallaxModifier(int layer)
        {
            float ret = 1.0f;
            if (this.parallaxModifier.ContainsKey(layer))
            {
                ret = this.parallaxModifier[layer];
            }

            return ret;
        }

        public void SetParallaxModifier(int layer, float modifier)
        {
            this.parallaxModifier[layer] = modifier;
        }

        public void Update(GameTime gameTime, RectangleF cameraBounds)
        {
            this.UpdateBounds();

            if (this.controller != null)
            {
                this.controller.Update(gameTime, this);
            }

            this.UpdateBounds();

            if (this.constraint != null)
            {
                this.Anchor = this.constraint.UpdatePosition(gameTime, this, this.Anchor);
            }

            this.UpdateBounds();
        }

        public Matrix GetCameraMatrix(int layer)
        {
            ////get
            ////{
            return Matrix.Identity *
                   Matrix.CreateTranslation(new Vector3(-this.Anchor * this.GetParallaxModifier(layer), 0)) *
                   Matrix.CreateScale(this.zoom) *
                   Matrix.CreateRotationZ(this.rotation) *
                   Matrix.CreateTranslation(new Vector3(this.Size * 0.5f, 0));
            ////}
        }

        public void UpdateBounds()
        {
            this.bounds.TopLeft = this.viewportPosition;
            this.bounds.BottomRight = this.viewportPosition + this.ViewportSize;
        }
    }
}
